package com.cm.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.common.core.Result;
import com.cm.order.entity.Cart;
import com.cm.order.entity.dto.CartKeyWordDTO;
import com.cm.order.mapper.CartMapper;
import com.cm.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @类 名:CartController </br>
 * @描 述:<br>
 * @日 期:2020/9/25 16:50 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartMapper cartMapper;
    @Autowired
    CartService cartService;
    @PostMapping("/insert")
    public Result insert (@RequestBody Cart cart){
        if (cartService.insert(cart)) {
            return Result.success("购物车添加成功");
        }
        return Result.fail(cart.getGoodsId() + "已存在！");
    }

    @PostMapping("/delete")
    public Result delete(Long id) {
        if (cartService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Cart cart) {
        if (cartService.update(cart)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/list")
    public Result getCart(@RequestBody CartKeyWordDTO cartKeyWordDTO) {
        IPage<Cart> ordersIPage = cartService.getCartList(cartKeyWordDTO);
        return Result.success(ordersIPage);
    }

    @GetMapping("/getById")
    public Result getById(Long id){
        Cart cart= cartMapper.selectById(id);
        return Result.success(cart);
    }
    @PostMapping("/getGoodsByUser")
    public Result getGoodsByUser(Long userId){
        return Result.success(cartService.getGoodsByUser(userId));
    }
    @PostMapping("/deleteByUG")
    public Result deleteByUg(Long userId,Long goodsId){
        if (cartService.deleteByUG(userId,goodsId))
            return Result.success("删除成功");
        return Result.fail("删除失败");
    }
}
